package cn.ittaoli.ajax.controller;

import cn.ittaoli.ajax.model.Admin;
import cn.ittaoli.ajax.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by lub_lenovo on 2017/8/2.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping( value = "/login",method = RequestMethod.GET)
    public void login(){

    }


    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String login(Model model, HttpSession session, Admin admin){
        Admin res=adminService.login(admin);
        if(res.getPwd().equals(admin.getPwd())){
            return "redirect:/article/articleList.do";
        }else{
            model.addAttribute("errorAdmin",res);
            model.addAttribute("msg","用户名或密码错误");
            return "admin/login";
        }

    }


    @RequestMapping("/adminList")
    public String adminList(Model model){
        List<Admin> admins=adminService.getAllAdmin();
        model.addAttribute("adminList",admins);
        model.addAttribute("menu","管理员列表");
        return "admin/adminList";
    }

    @RequestMapping("/adminAdd")
    public String adminAdd(Model model){
        model.addAttribute("menu","新增管理员");
        return "admin/adminAdd";
    }

    @RequestMapping("/insertAdmin")
    public String insertAdmin(Model model,Admin admin){
//        System.out.println(new Date().getTime());

        int res=adminService.insertAdmin(admin);
        if(res>0){
            return "redirect:adminList.do";
        }
        return "admin/adminAdd";
    }

}
